package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Override
    public PageResult PageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<Dish> page = dishMapper.PageQuery(dishPageQueryDTO);
        long total = page.getTotal();
        List<Dish> records = page.getResult();
        PageResult pageResult = new PageResult();
        pageResult.setTotal(total);
        pageResult.setRecords(records);
        return pageResult;
    }

    /**
     * 修改菜品
     * @param dishDTO
     */
    @Override
    @Transactional
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);

        //修改菜品表信息
        dishMapper.update(dish);
        //删除原有的口味数据
        dishFlavorMapper.deleteByDishId(dish.getId());
        //重新插入口味数据
        List<DishFlavor> dishFlavors = dishDTO.getFlavors();
        if (dishFlavors!=null && dishFlavors.size()>0){
            dishFlavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishDTO.getId());
            });
        }
        dishFlavorMapper.insertBatch(dishFlavors);
    }

    @Override
    public DishVO getDishById(Long id) {
        DishVO dishVO = new DishVO();
        //根据id查询
        Dish dish = dishMapper.getByid(id);
        //根据菜品ID查询口味表
        List<DishFlavor> dishFlavor = dishFlavorMapper.getByDishId(dish.getId());
        //将查询到的数据封装到DishVo中

        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavor);
        return dishVO;
    }

    /**
     * 菜品的批量删除
     * @param ids
     */
    @Override
    @Transactional
    public void delateBatch(List<Long> ids) {
        //查询当前菜品的状态，起售状态1不允许删除
        for (Long id : ids) {
            Dish dish = dishMapper.getByid(id);
            if (dish.getStatus()== StatusConstant.ENABLE){
                //当前菜品处于起售状态,不能删除
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);


            }
        }
        //判断菜品是否被关联进套餐表，在套餐表里的菜品不允许删除
        List<Long> setmealIdByDishId = setmealDishMapper.getSetmealIdByDishId(ids);
        if (setmealIdByDishId!=null&&setmealIdByDishId.size()>0){
            //当前菜品被关联龙，不允许删除
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //删除菜品表中的菜品信息
        for (Long id : ids) {
            dishMapper.Delate(id);
        }

        //删除菜品关联的口味表数据
        for (Long id : ids) {
            dishFlavorMapper.deleteByDishId(id);
        }




    }

    @Override
    @Transactional
    public void saveWithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        //向菜品表添加数据
        dishMapper.insert(dish);
        //获取insert语句生成的ID值
        Long id = dish.getId();

        //向口味表添加数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors!=null&&flavors.size()>0){
            //给每一条口味都插入ID
            flavors.forEach(dishFlavor ->{
                dishFlavor.setDishId(id);
            });

            //向口味表添加数据
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    @Override
    public void DisableWithStart(Integer status, Long id) {
        Dish dish = new Dish();
        dish.setId(id);
        dish.setStatus(status);
        dishMapper.update(dish);
    }

    @Override
    public List<Dish> FindDishByDishId(Integer id) {
        List<Dish> dishList = dishMapper.FindDishByDishId(id);
        return dishList;
    }

    @Override
    public List<DishVO> GetUserDishList(Long categoryId) {
        List<DishVO> dishVOList= new ArrayList<>();
        List<Dish> dishList= dishMapper.GetUserDishList(categoryId);

        dishList.forEach(dish -> {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(dish,dishVO);
            dishVO.setFlavors(dishFlavorMapper.getByDishId(dish.getId()));
            dishVOList.add(dishVO);
        });
        return dishVOList;
    }
}
